/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.showcase;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import javax.swing.JPanel;

public class CheckerPanel extends JPanel
{

    private static final long serialVersionUID = -7959603276153326315L;

    private static final int CHECKERBOARD_SIZE = 16;
    private static final int TILE_SIZE = 8;

    private final BufferedImage checkerboard;

    public CheckerPanel()
    {
        this(Color.BLACK, Color.WHITE);
    }

    public CheckerPanel(Color firstColor, Color secondColor)
    {
        super();

        setOpaque(false);
        checkerboard = new BufferedImage(CHECKERBOARD_SIZE, CHECKERBOARD_SIZE, BufferedImage.TYPE_INT_ARGB);

        WritableRaster raster = checkerboard.getRaster();
        int[] firstColorValues =
            {firstColor.getRed(), firstColor.getGreen(), firstColor.getBlue(), firstColor.getAlpha()};
        int[] secondColorValues =
            {secondColor.getRed(), secondColor.getGreen(), secondColor.getBlue(), secondColor.getAlpha()};

        boolean even = true;

        for (int y = 0; y < CHECKERBOARD_SIZE; y += 1)
        {
            for (int x = 0; x < CHECKERBOARD_SIZE; x += 1)
            {
                raster.setPixel(x, y, (even) ? firstColorValues : secondColorValues);
                even = !even;
            }

            even = !even;
        }
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        int width = getWidth();
        int height = getHeight();

        for (int y = 0; y < height; y += TILE_SIZE * CHECKERBOARD_SIZE)
        {
            for (int x = 0; x < width; x += TILE_SIZE * CHECKERBOARD_SIZE)
            {
                g.drawImage(checkerboard, x, y, x + (TILE_SIZE * CHECKERBOARD_SIZE), y
                    + (TILE_SIZE * CHECKERBOARD_SIZE), 0, 0, CHECKERBOARD_SIZE, CHECKERBOARD_SIZE, this);
            }
        }

        super.paintComponent(g);
    }
}
